
var PageCount, PageSize=8,page,month;//设置每页示数目;
function NewData(start,length,selectPage){	
	var selectPage=parseInt(selectPage);
    //console.log(start,length,selectPage);
    var Sta=start,End=length;
    $.ajax({
        type:"post",
        url:"http://huodong.lingwangyun.cn/News/igetNewsList",
        dataType:"JSON",
        data:{start:Sta,length:End},
        success:function(info){
            if(info.code==0){
                page=Math.ceil(info.data.page-1)%PageSize;//最后一条数据 参数
                PageCount= Math.ceil((info.data.page-1)/PageSize);//页面总数
                var data=info.data.data,html='';
                var j=0;
                if(Sta==0){
                    j=1;
                    var span=info.data.data[0].text;
                    if(span.length>128){
                        span=span.substring(0,128)+"...";
                    }
                    //第一条新闻加载
					var nohtml='<div>'+info.data.data[0].title+'</div><span>'+span+'</span><div  myId="'+info.data.data[0].id+'" class="move">move</div>';
                    $('.ajax>.top>.lidata').html(nohtml);
                    if(!info.data.data[0].filepath){
                        $('.CH-cont>div.cont>.ajax>.top>.img').css('background-image','url(../../images/news/moren.png)');//html(`<img src="images/news/moren.png" alt="">`);
                    }else{
                        $('.CH-cont>div.cont>.ajax>.top>.img').css('background-image','url('+info.data.data[0].filepath+')');//html(`<img src="${info.data.data[0].filepath}" alt="">`);
                    }
                }
                for(var i=j;i<info.data.data.length;i++){
                    var text=info.data.data[i].text;
                    if(text.length>75){
                        text=text.substring(0,75)+"...";
                    }
                    /**/
                    var  moo = ['Jan', 'Feb', 'Mar', ' Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
                    month= moo[parseInt(info.data.data[i].month,10)-1];
                    /**/
                    html+='<div class="chi" myId="'+info.data.data[i].id+'" day="'+info.data.data[i].day+'" month="'+info.data.data[i].month+'">';
                    html+='<div><div class="eng">'+month+'</div><div class="num">'+info.data.data[i].day+'</div><div class="bottom"></div></div>';
                    html+='<div class="text"><div>'+info.data.data[i].title+'</div><span>'+text+'</span></div></div>';
                }
                $('.ajax>.acont').html(html);
                $('.ajax>.acont>div.chi').hover(
                    function(){
                        var $this=$(this);
                        var month=$this.attr('month'),day=$this.attr('day');
                        var arr = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
                        month= arr[parseInt(month,10)-1]+'月';
                        $this.children('div:first-child').addClass('hover');
                        var hover='<div class="seven">'+day+'</div><div class="hua">'+month+'</div><div class="bottom"></div>';
                        $this.children('div:first-child').html(hover);
                        $this.children('div.text').children('div').addClass('hover');
                    }
                    ,function(){
                        var $this=$(this);
                        var day=$this.attr('day');
                        $this.children('div:first-child').removeClass('hover');
                        var hover='<div class="eng">'+month+'</div><div class="num">'+day+'</div><div class="bottom"></div>';
                        $this.children('div:first-child').html(hover);
                        $this.children('div.text').children('div').removeClass('hover');
                    }
                );
                /**/
                var pp="<span>共"+PageCount+"页</span>";
                //console.log('pp',pp);
                $('.CH-cont>div>div.button>ul.Bright>li.gong').html(pp);
                //造个简单的分页按钮
                var li='<li id="Blast" class="lr"><span>上一页</span></li>';
                if(PageCount<10){
                    for(var i=1;i<PageCount+1;i++){
                        li+='<li selectPage="'+i+'"><span>'+i+'</span></li>';
                    }
                }else{
                    for(var i=1;i<9;i++){
                        li+='<li selectPage="'+i+'}"><span>'+i+'</span></li>';
                    }
                    li+='<li class="dian"> ...</li><li selectPage="'+PageCount+'"><span>'+PageCount+'</span></li>';
                }
                li+='<li id="Bnext" class="lr"><span>下一页</span></li>';
                $('.button>ul.Bleft').html(li);
                /*****************************************************************************/
                $('.CH-cont>div>div.button>ul.Bleft>li').removeClass('yes');
				var	yes='.CH-cont>div>div.button>ul.Bleft>li:nth-child('+(selectPage+1)+')';
				//console.log('yes',yes);
                $(yes).addClass('yes');
                if(selectPage>1){
                    $('.CH-cont>div>div.button>ul.Bleft>#Blast').show();
                }else{
                    $('.CH-cont>div>div.button>ul.Bleft>#Blast').hide();
                }
                if(selectPage<PageCount){
                    $('.CH-cont>div>div.button>ul.Bleft>#Bnext').show();
                }else{
                    $('.CH-cont>div>div.button>ul.Bleft>#Bnext').hide();
                }
            }
            if(selectPage>4){
			 var hide='ul.Bleft>li:nth-child('+(selectPage-4)+')';
             $(hide).prevAll().hide();
			 var dian='<li class="dian">...</li>';
             $(hide).before(dian);
             $('ul.Bleft>#Blast').show();
             }
             if((PageCount-selectPage)>4){
             var hide='ul.Bleft>li:nth-child('+(selectPage+4)+')';
             $(hide).nextAll().hide();
			 var dian='<li class="dian">...</li>';
             $(hide).after(dian);
             $('ul.Bleft>#Bnext').show();
             }
        }
    })
}
NewData(0,9,1);

//点击数字按钮
$('.button').on('click','ul.Bleft>li:not(#Blast,#Bnext)',function(){
    var selectPage=parseInt($(this).attr('selectpage'));
    NewData(((selectPage-1)*8)+1,selectPage*8,selectPage);
});
//点击上一页
$('.button').on('click','ul.Bleft>#Blast',function(){
    var selectPage=parseInt($('.CH-cont>div>div.button>ul>li.yes').attr('selectpage'))-1;
   // console.log(selectPage);
    NewData(((selectPage-1)*8)+1,selectPage*8,selectPage);
})
//点击下一页
$('.button').on('click','ul.Bleft>#Bnext',function(){
    var selectPage=parseInt($('.CH-cont>div>div.button>ul>li.yes').attr('selectpage'))+1;
    NewData(((selectPage-1)*8)+1,selectPage*8,selectPage);
})
//点击确认
$('.button').on('click','ul.Bright>li.ok',function(){
    var selectPage=parseInt($('.CH-cont>div>div.button>ul.Bright>li.ye>input').val());
   // console.log('输入款',selectPage,'总',PageCount);
    if(selectPage==''){
        //跳转到第一页
        NewData(0,9,1);
    }else  if(selectPage<1){
        //跳转到第一页
        NewData(0,9,1);
    }else if(selectPage>PageCount){
        //跳转到最后一页
        if(page==0){
            NewData(((PageCount-1)*8)+1,((PageCount-1)*8)+8,PageCount);
        }else{
            NewData(((PageCount-1)*8)+1,((PageCount-1)*8)+page,PageCount);
        }
    }else{
        //正常跳转
        NewData(((selectPage-1)*8)+1,selectPage*8,selectPage);
    }
    $('.CH-cont>div>div.button>ul.Bright>li.ye>input').val('');
})
//单个新闻点击
$('.CH-cont>div.cont>.ajax').on('click','.top>div>.move',function(){
    var $this=$(this);
    var id=$this.attr('myId');
    window.location.href='newDetails.html?id='+id;
});
$('.CH-cont>div.cont>.ajax>.acont').on('click','div.chi',function(){
    var $this=$(this);
    var id=$this.attr('myId');
    window.location.href='newDetails.html?id='+id;
});